package cn.wp.ewallet.upms.dao.impl;

import cn.wp.common.core.dao.JdbcSupportTemplate;
import cn.wp.common.core.dao.SqlString;
import cn.wp.common.core.model.WpPage;
import cn.wp.ewallet.upms.dao.PaUserInfoDao;
import cn.wp.ewallet.upms.model.entity.PaUserInfoDo;
import cn.wp.ewallet.upms.model.entity.PaUserInfoDoo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户数据访问层
 * create by ganjie
 */

@Repository
public class PaUserInfoDaoImpl implements PaUserInfoDao {

    @Autowired
    private JdbcSupportTemplate jdbcSupportTemplate;

    @Override
    public List<PaUserInfoDo> selectPaUserInfoByPhone(String phone) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select *");
        sqlString.add("from pa_user_info where user_tel = ?");
        return jdbcSupportTemplate.selectList(sqlString.build(),new Object[]{phone},PaUserInfoDo.class);
    }

    @Override
    public void insertPaUserInfo(PaUserInfoDo paUserInfoDo) {
        jdbcSupportTemplate.insert(paUserInfoDo);

    }

    @Override
    public WpPage<PaUserInfoDoo> selectPaUserInfoList(String userTel, WpPage wpPage) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select u.USER_ID, u.USER_TEL, u.MOBILE_PREFIX, r.WALLET_ADDRESS, r.CREATE_DATE");
        sqlString.add("from pa_user_info u, user_wallet_rel r");
        sqlString.add("where u.USER_ID = r.USER_ID");
        List<String> params = new ArrayList<>();
        if(StringUtils.isNotEmpty(userTel)) {
            sqlString.add("    and USER_TEL like ?");
            params.add("%"+userTel+"%");
        }
        sqlString.add("ORDER BY r.CREATE_DATE DESC");
        return jdbcSupportTemplate.selectList(sqlString.build(), params.toArray(), wpPage, PaUserInfoDoo.class);
    }
}
